public class work2 {
    // 力扣：1658. 将 x 减到 0 的最小操作数
    public  static int minOperations(int[] nums, int x) {
        long sum=0;
        for(int i=0;i<nums.length;i++){
            sum+=nums[i];
        }
        //防止出现全部（注意特殊情况）
        if(sum==x){
            return nums.length;
        }
        long target=(long)sum-x;
        int left=0,right=0;
        long ret=0;//实时记录
        int max=0;
        while(right<nums.length){
            ret+=nums[right];
            if(ret>target){
                while(left<right&&ret>target){
                    ret-=nums[left++];
                }
            }
            if(ret==target){
                max=Math.max(max,right-left+1);
                ret-=nums[left++];
            }
            right++;
        }
        return max==0? -1:nums.length-max;
    }

    public static void main(String[] args) {
        int []nums={8828,9581,49,9818,9974,9869,9991,10000,10000,10000,9999,9993,9904,8819,1231,6309};
        System.out.println(minOperations(nums,134365));
    }
}